package com.wwp.tbshop.activity;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.itheima.pulltorefreshlib.PullToRefreshBase;
import com.itheima.pulltorefreshlib.PullToRefreshListView;
import com.wwp.tbshop.R;
import com.wwp.tbshop.event.SaleNewsCartEventbus;
import com.wwp.tbshop.model.LimitBuyPlvAdapter;
import com.wwp.tbshop.model.bean.LimitBuyBean;
import com.wwp.tbshop.presenter.LimitBuyPresenter;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * Created by ${wenping} on 6/16/2017.
 */

public class LimitBuyActivity extends BaseActivity {

    private int PAGE = 1;
    @Bind(R.id.sale_news_loading)
    RelativeLayout mSaleNewsLoading;
    @Bind(R.id.limt_buy_back)
    ImageView mLimtBuyBack;
    @Bind(R.id.limt_buy_cart)
    ImageView mLimtBuyCart;
    @Bind(R.id.limt_buy_recycleview)
    RecyclerView mLimtBuyRecycleview;

    @Bind(R.id.limit_buy_plv)
    PullToRefreshListView mLimitBuyPlv;

/*    private LimitBuyAdapater mLimitBuyAdapater;
    private ListViewAdapter mAdapter;//这是listview对应的adapter(没有实际使用);*/
    private LimitBuyPlvAdapter mLimitBuyPlvAdapter;
    private LimitBuyPresenter mMLimitButPresenter;

    @Override
    public int getLayoutId() {
        return R.layout.activity_limit_buy;
    }

    @Override
    protected void init() {
        super.init();

        mMLimitButPresenter = new LimitBuyPresenter(this);
        mMLimitButPresenter.loadLimitBuyGoods(PAGE, 10);

 /*       mLimitBuyAdapater = new LimitBuyAdapater(this,this);
        mLimtBuyRecycleview.setLayoutManager(new LinearLayoutManager(this));
        mLimtBuyRecycleview.setAdapter(mLimitBuyAdapater);*/


        mLimitBuyPlvAdapter = new LimitBuyPlvAdapter(this);
        mLimitBuyPlv.setAdapter(mLimitBuyPlvAdapter);
        mLimitBuyPlv.setMode(PullToRefreshBase.Mode.PULL_FROM_END);
        mLimitBuyPlv.setOnRefreshListener(mOnRefreshListener);

        mLimitBuyPlv.setOnItemClickListener(itemClickListener);
    }

    private static final String TAG = "LimitBuyActivity";

    PullToRefreshBase.OnRefreshListener2 mOnRefreshListener = new PullToRefreshBase.OnRefreshListener2() {
        @Override
        public void onPullDownToRefresh(PullToRefreshBase base) {

        }

        @Override
        public void onPullUpToRefresh(PullToRefreshBase base) {
            mLimitBuyPlv.postDelayed(new Runnable() {
                @Override
                public void run() {
                    PAGE++;
                    mMLimitButPresenter.loadLimitBuyGoods(PAGE, 10);
                    mLimitBuyPlv.onRefreshComplete();
                }
            }, 1000);
        }
    };
    AdapterView.OnItemClickListener itemClickListener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            //实现跳转和实现;
        }
    };

    @OnClick({R.id.limt_buy_back, R.id.limt_buy_cart})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.limt_buy_back:
                finish();
                break;
            case R.id.limt_buy_cart:
                //跳转到购物车;有问题呢;
                SaleNewsCartEventbus eventbus = new SaleNewsCartEventbus();
                eventbus.SalenewsToMain(this);
                EventBus.getDefault().post(eventbus);

                break;
        }
    }

    private List<LimitBuyBean.ProductListBean> mProductList = new ArrayList<>();

    public void onLimitBuySuccess(List<LimitBuyBean.ProductListBean> list) {
        if (list != null) {
            if (mLimitBuyPlvAdapter.mCountDownTimer!=null){
                mLimitBuyPlvAdapter.mCountDownTimer.cancel();
            }
                //mLimitBuyPlvAdapter.start();
                mLimitBuyPlvAdapter.startCountDown();//开始倒计时
                mProductList.addAll(list);
                mLimitBuyPlvAdapter.setProductListBeanList(mProductList);
                //Toast.makeText(this, list.size() + "", Toast.LENGTH_LONG).show();

        }else{
            return;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        //mLimitBuyAdapater.stopCountDown();
//        mLimitBuyPlvAdapter.stop();
        if (mLimitBuyPlvAdapter.mCountDownTimer!=null){
        mLimitBuyPlvAdapter.stopCountDown();
        }
    }
}
